﻿/*
 * Created by SharpDevelop.
 * User: StrunevskyDV
 * Date: 03.03.2010
 * Time: 20:00
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using EmailJabber.Logic.Commands.Base;
using EmailJabber.Logic;
using EmailJabber.Logic.Logic.BusinessLogic;
using FinancialPersonal.Entities;
using System.Collections.Generic;

namespace FinancialPersonal.Commands
{
    /// <summary>
    /// Description of WeekendMessage.
    /// </summary>
    public class WeekendMessage : CommandBase
    {
        public static Guid ID = new Guid("134A0CA3-81A1-4382-82E4-937BB306B979");

        protected override string GetDecription()
        {
            return "Speaking with you every week";
        }

        protected override Guid GetId()
        {
            return ID;
        }

        /// <summary>
        /// Constructor
        /// Set timer command on register command
        /// </summary>
        public WeekendMessage()
        {
            CommandProcessor.Instance.PushCommandOnTime(ID, DateTime.Now.AddMinutes(1));
        }

        protected override void ExecuteSafe(MessageIM comnd)
        {
            IList<UserEntity> users = Logic.FinanceLogic.Instance.GetAllUsers();
            foreach (UserEntity user in users)
            {
            	if (((DateTime.Now - user.LastWeekendMessageSend).TotalDays > 7)	
            	    &&(DateTime.Now.DayOfWeek.Equals(DayOfWeek.Sunday)))
                {                    
                    string body = "Доставайте уже следующий конверт"+Environment.NewLine;
                    body += "то что осталось в текущем можно потратить или отложить в сбережения."+Environment.NewLine;
                    user.LastWeekendMessageSend = DateTime.Now;
                    EntitiesBlo.Instance.StoreEntity(user);
                    BL.Instance.Send(new MessageIM(comnd.From, body, user.Jid));
                }
            }
            CommandProcessor.Instance.PushCommandOnTime(ID, DateTime.Now.AddMinutes(10));
        }

    }
}
